package com.iraeta.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.entity.Irascstepmodel;
import com.iraeta.entity.Irascstepmodelmx;
import com.iraeta.service.iraorder.IraScStepModelService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/*
 * 内部用户维护生产步骤模版功能
 * */
@RestController(value = "IraScStepModelController")
@RequestMapping("emp/stepmodel")
public class IraScStepModelController extends BaseController{
    @Resource
    IraScStepModelService iraScStepModelService;

    @RequestMapping("pagemodel")
    public String pageModel(@RequestBody JSONObject jo){
        this.pageSet(jo);
        List<Irascstepmodel> irascstepmodelList = iraScStepModelService.listIraScStepModel();
        PageInfo<Irascstepmodel> irascstepmodelPageInfo = this.getPageInfo(irascstepmodelList);
        return this.success(irascstepmodelPageInfo);
    }

    @RequestMapping("listmodel")
    public String pageModel(){
        List<Irascstepmodel> irascstepmodelList = iraScStepModelService.listIraScStepModel();
        return this.success(irascstepmodelList);
    }

    @RequestMapping("getmodel")
    public String getModel(@RequestBody JSONObject paramJb){
        int modelId = paramJb.getIntValue("id");
        Irascstepmodel iraScStepModel = iraScStepModelService.getIraScStepModel(modelId);
        return this.success(iraScStepModel);
    }

    @RequestMapping("addeditmodel")
    public String addEditOrder(@RequestBody JSONObject paramJb){
        int modelId = paramJb.getIntValue("id");
        Irascstepmodel iraScStepModel = modelId == 0 ? new Irascstepmodel() : iraScStepModelService.getIraScStepModel(modelId);
        this.mapJsonToObj(paramJb, iraScStepModel);
        JSONArray modelMxJSONArray = paramJb.getJSONArray("modelmxList");
        if(modelId == 0){
            iraScStepModelService.addIraScStepModel(iraScStepModel, modelMxJSONArray);
        }else {
            iraScStepModelService.updateIraScStepModel(iraScStepModel, modelMxJSONArray);
        }
        return this.success();
    }

    @RequestMapping("listmodelmx")
    public String listModelMx(@RequestBody JSONObject paramJb){
        int modelId = paramJb.getIntValue("id");
        List<Irascstepmodelmx> irascstepmodelmxList = iraScStepModelService.listIraScStepModelMx(modelId);
        return this.success(irascstepmodelmxList);
    }
    @RequestMapping("deletemodelmx")
    public String deleteModelMx(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        iraScStepModelService.deleteIraScStepModelMx(id);
        return this.success();
    }
}
